/**
 * Created by Administrator on 2017/9/20.
 */

angular.module('tableMedia', [
        'ngStorage',
        'ui.router',
        'ngFileUpload',
        'ngAnimate',
        'ng.comfirm',
        'service.fileupload',
        'tableMedia.filter',
        'module.recommended',
        'component.alert',
        'module.advertisement',
        'module.auth',
        'module.charge',
        'module.news',
        'module.repair',
        'module.saleStatistics',
        'module.set',
        'module.table',
        'module.template',
    ])

    .run(
        ['$rootScope', '$localStorage', '$state', '$http', '$stateParams', '$interval', 'AuthService',
            function ($rootScope, $localStorage, $state, $http, $stateParams, $interval, AuthService) {
                $rootScope.$state = $state;
                $rootScope.$stateParams = $stateParams;
                if ($localStorage[environmental + "shopid"] && AuthService.isAuthenticated()) {
                    var shopid = $localStorage[environmental + "shopid"];
                    var client = mqtt.connect('ws://120.27.192.219:3000');
                    client.subscribe("/2/4/" + shopid);
                    client.on("message", function (topic, payload) {
                        var message = JSON.parse(payload);
                        var changebody = JSON.parse(message.body.body).msg;
                        if (JSON.parse(message.body.body).msg) {
                            changebody = JSON.parse(message.body.body).tablename + JSON.parse(message.body.body).msg;
                        }
                        var zhText = encodeURI(changebody);
                        var audio = new Audio("http://tts.baidu.com/text2audio?lan=zh&ie=UTF-8&spd=2&text=" + zhText);
                        document.addEventListener("WeixinJSBridgeReady", function () {
                            audio.play();
                        }, false);
                    });
                }
                $rootScope.$on('$stateChangeStart', function (event, toState, toParams, fromState) {
                    if (toState.name == "login" && fromState.name == "homepage" && AuthService.isAuthenticated()) {
                        event.preventDefault();
                    }
                    if (toState.name == "main" && fromState.name == "login") {
                        event.preventDefault();
                        $state.go("login");
                    }
                });


            }]
    )
    .config(
        ['$stateProvider', '$urlRouterProvider', '$locationProvider', '$httpProvider',
            function ($stateProvider, $urlRouterProvider, $locationProvider, $httpProvider) {
                $urlRouterProvider.otherwise('/login');
                $locationProvider.html5Mode(html5mode);
                $httpProvider.defaults.withCredentials = true;
            }
        ]
    );